package com.ejlchina.tool.fragment.offline

import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.adolf.androiddemo.databinding.FragmentLightAndVolumeBinding
import com.adolf.androiddemo.databinding.FragmentVolumeOfflineBinding
import com.huyuhui.fastble.data.BleDevice
import com.ejlchina.tool.adapter.DeviceAdapterRadio
import com.ejlchina.tool.adapter.DeviceAdapterRadioOffline
import com.ejlchina.tool.adapter.LightTimeAdapter
import com.ejlchina.tool.base.BaseBindingFragment2
import com.ejlchina.tool.bean.BleDeviceBean
import com.ejlchina.tool.bean.LightTimeBean
import com.ejlchina.tool.bean.TimeBean
import com.ejlchina.tool.enums.LightTimeGetEnum
import com.ejlchina.tool.enums.OnOffEnum
import com.ejlchina.tool.singleton.GlobalConnectObject
import com.ejlchina.tool.util.COMMAND_LIGHT_TIME_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_LIGHT_TIME_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_LIGHT_TIME_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_LIGHT_TIME_SET_UPWARD
import com.ejlchina.tool.util.COMMAND_VOLUME_GET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_VOLUME_GET_UPWARD
import com.ejlchina.tool.util.COMMAND_VOLUME_SET_DOWNWARD
import com.ejlchina.tool.util.COMMAND_VOLUME_SET_OFFLINE_UPWARD
import com.ejlchina.tool.util.COMMAND_VOLUME_SET_UPWARD
import com.ejlchina.tool.util.LOG_TAG
import com.ejlchina.tool.util.bytearray.intTo16HexStringLE
import com.ejlchina.tool.util.bytearray.intToByteHexString
import com.ejlchina.tool.util.bytearray.readUInt16LE
import com.ejlchina.tool.view.GridItemDecoration
import com.ejlchina.tool.view.HorizontalItemDecoration
import com.litao.slider.effect.ITEffect
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Calendar

class VolumeOfflineFragment: BaseBindingFragment2<FragmentVolumeOfflineBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initData()
    }

    private fun initViews(){
        setHeadTitle("音量设置")
        setRightIconVisibility(View.GONE)
        initListeners()
    }

    private fun initDevices(){
        deviceAdapterOffline = DeviceAdapterRadioOffline(GlobalConnectObject.getCurrentConnectDeviceList()){
            viewLifecycleOwner.lifecycleScope.launch {
                getLightData(it)
                delay(600)
                getVolumeData(it)
            }
        }
        binding.rvDevices.apply {
            adapter = deviceAdapterOffline
            layoutManager = GridLayoutManager(requireContext(), 3)
            if (itemDecorationCount == 0) {
                addItemDecoration(GridItemDecoration(3, 11, 11))
            }
        }
    }

    private fun initListeners(){

        binding.btnConfirm.setOnClickListener {
            setVolumeData(deviceAdapterOffline.getSelectDevice())
        }
    }

    override fun whenReceiveCommand(bleDevice: BleDeviceBean,command: String, dataByteArray: ByteArray) {
        super.whenReceiveCommand(bleDevice,command, dataByteArray)
        dismissLoading()
        when(command){
            COMMAND_VOLUME_GET_UPWARD -> {
                binding.slideVolume.setValue(dataByteArray[0].toInt().toFloat(), true)
                dismissLoading()
            }

            COMMAND_VOLUME_SET_OFFLINE_UPWARD -> {
                dismissLoading()
                setResultToast(dataByteArray[dataByteArray.size - 1].toInt())
            }
        }
    }

    private fun initData(){
        initDevices()
        binding.slideVolume.apply {
            effect = ITEffect(this).apply {
                valueFrom = 0F
                valueTo = 15F
            }
            addOnIntValueChangeListener { slider, value, fromUser ->
                setThumbText(value.toString())
            }
        }
    }

    private fun getLightData(bleDevice: BleDeviceBean){
        getCommandData(bleDevice, COMMAND_LIGHT_TIME_GET_DOWNWARD)
    }

    private fun getVolumeData(bleDevice: BleDeviceBean){
        getCommandData(bleDevice, COMMAND_VOLUME_GET_DOWNWARD)
    }

    private fun setVolumeData(bleDevice: BleDeviceBean){
        setCommandData(bleDevice,COMMAND_VOLUME_SET_DOWNWARD, getVolumeResultData())
    }



    private fun getVolumeResultData(): String{
        return intToByteHexString(binding.slideVolume.value.toInt())
    }
    override fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): FragmentVolumeOfflineBinding {
        return FragmentVolumeOfflineBinding.inflate(inflater,container,true)
    }
}